#include "library.h"
#include <vector>

_library::_library()
{
    user admin("admin","admin");
    admin.isadmin = 1;
    users.adduser(admin);
}

void demo()
{
    while(1)
    {
        clearUI();
        //PrintUI();
        int m_key_pressed = KeyboardOperate();
        if(m_key_pressed == KEY_UP)
        {

        }
        else if(m_key_pressed == KEY_DOWN)
        {
            
        }
        else if(m_key_pressed == KEY_ESC)
        {

        }
        else
        {

        }
    }
}

void _library::mainpage()
{
    //先把UI定义写了
    std::vector<_UI> mainpage;

    _UI mainpage1;
    mainpage1.type = UI_str;
    mainpage1.addconststring("欢迎来到图书馆，请登录或注册",1);
    mainpage1.addconststring("请选择登录身份：",1);
    mainpage.emplace_back(mainpage1);

    _UI identifierselector;
    identifierselector.type = UI_selector;
    identifierselector.selectNum = 0;
    identifierselector.specialSelector = 0;
    identifierselector.addconststring("游客",1);
    identifierselector.addconststring("普通用户",1);
    identifierselector.addconststring("管理员",1);
    identifierselector.addconststring("用户登陆",1);
    identifierselector.addconststring("用户注册",1);
    mainpage.emplace_back(identifierselector);

    //通过大循环，打印UI
    while(1)
    {
        //首先先把UI显示出来
        clearUI();
        PrintUI(mainpage);

        //检查一下键盘操作
        int m_key_pressed = KeyboardOperate();
        if(m_key_pressed == KEY_UP)
        {
            //键盘上键被按下
            //这里只有一个selector，所以就不用计算多selecotr之间的光标移动了
            //如果是按下上键，并且光标已经在最上边了，那就移动到最下边，否则光标向上移动
            if(mainpage[1].selectNum == 0)
                mainpage[1].selectNum = mainpage[1].data.size() - 2;
            else
                mainpage[1].selectNum -= 1;
        }
        else if(m_key_pressed == KEY_DOWN)
        {
            //键盘下键被按下
            //这里只有一个selector，所以就不用计算多selecotr之间的光标移动了
            //如果是按下下键，并且光标已经在最下边了，那就移动到最上边，否则光标向下移动
            if(mainpage[1].selectNum == mainpage[1].data.size() - 2)
                mainpage[1].selectNum = 0;
            else
                mainpage[1].selectNum += 1;
        }
        else if(m_key_pressed == KEY_ESC)
        {
            //键盘esc被按下
            //那就回家喽
            return;
        }
        else
        {
            //键盘回车被按下
            //这里只有一个selector，那就直接对这个selector调功能就好了
            if(mainpage[1].selectNum == 3)
            {
                //这就是进登录页面了
                loginpage(mainpage[1].specialSelector);
            }
            else if(mainpage[1].selectNum == 4)
            {
                registerpage(0);
            }
            else
            {
                
                //这就是常规选项
                mainpage[1].specialSelector = mainpage[1].selectNum;
            }
        }
    }
}

void _library::registerpage(int type)
{
    std::vector<_UI> _registerpage;
    std::string username;
    std::string password;

    _UI tittle;
    tittle.type = UI_str;
    if(type == 0)
    {
        tittle.addconststring("用户注册",1);
    }
    else
    {
        tittle.addconststring("管理员注册",1);
    }
    _registerpage.emplace_back(tittle);

    _UI selector;
    selector.type = UI_selector;
    selector.specialSelector = -1;
    selector.addconststring("用户名:");
    selector.addstr(&username,1);
    selector.addconststring("密码:");
    selector.addstr(&password,1);
    selector.addconststring("确认注册",1);
    _registerpage.emplace_back(selector);

    bool lasterr = 0;

    while(1)
    {
        clearUI();
        PrintUI(_registerpage);

        int m_key_pressed = KeyboardOperate();
        if(m_key_pressed == KEY_UP)
        {
            if(_registerpage[1].selectNum == 0)
                _registerpage[1].selectNum = _registerpage[1].data.size() - 2;
            else
                _registerpage[1].selectNum -= 1;
        }
        else if(m_key_pressed == KEY_DOWN)
        {
            if(_registerpage[1].selectNum == _registerpage[1].data.size() - 2)
                _registerpage[1].selectNum = 0;
            else
                _registerpage[1].selectNum += 1;
        }
        else if(m_key_pressed == KEY_ESC)
        {
            return;
        }
        else
        {
            if(_registerpage[1].selectNum == 0)
            {
                std::cout << "请输入用户名：";
                std::cin >> username;
            }
            else if(_registerpage[1].selectNum == 1)
            {
                std::cout << "请输入密码：";
                std::cin >> password;
            }
            else if(_registerpage[1].selectNum == 2)
            {
                if(username.empty() || password.empty())
                {
                    if(!lasterr)
                    {
                        _UI err;
                        err.type = UI_str;
                        err.addconststring("用户名密码不能为空");
                        _registerpage.emplace_back(err);
                    }
                }
                else
                {
                    if(users.adduser(user(username,password)))
                        return;
                    else
                    {
                        _UI err;
                        err.type = UI_str;
                        err.addconststring("该用户名已经被占用");
                        _registerpage.emplace_back(err);
                    }
                }
            }
        }
    }
}

void _library::loginpage(int type)
{
    //先判断一下要不要直接进游客模式
    if(type == 0)
        userpage(1);

    std::string username;
    std::string password;
    bool last_fail = 0;
    
    //先把UI定义喽
    std::vector<_UI> loginpage;

    _UI m_tittle;
    m_tittle.type = UI_str;
    m_tittle.addconststring("登陆界面",1);
    loginpage.emplace_back(m_tittle);
    
    _UI m_loginselector;
    m_loginselector.type = UI_selector;
    m_loginselector.addconststring("用户名:");
    m_loginselector.addstr(&username,1);
    m_loginselector.addconststring("密码:");
    m_loginselector.addstr(&password,1);
    m_loginselector.addconststring("登陆");
    loginpage.emplace_back(m_loginselector);

    //大循环进入功能
    while(1)
    {
        clearUI();
        PrintUI(loginpage);
        int m_key_pressed = KeyboardOperate();
        std::cout << "\n" << m_key_pressed << "\n";
        if(m_key_pressed == KEY_UP)
        {
            if(loginpage[1].selectNum == 0)
                loginpage[1].selectNum = loginpage[1].data.size() - 1;
            else
                loginpage[1].selectNum -= 1;
        }
        else if(m_key_pressed == KEY_DOWN)
        {
            if(loginpage[1].selectNum == loginpage[1].data.size() - 1)
                loginpage[1].selectNum = 0;
            else
                loginpage[1].selectNum += 1;
        }
        else if(m_key_pressed == KEY_ESC)
        {
            return;
        }
        else
        {
            if(loginpage[1].selectNum == 0)
            {
                std::cout << "请输入用户名:";
                std::cin >> username;
            }
            else if(loginpage[1].selectNum == 1)
            {
                std::cout << "请输入密码:";
                std::cin >> password;
            }
            else
            {
                //判断一下账号密码对不对
                bool success = users.login(username,password);
                if(success == 0 && last_fail == 0)
                {
                    _UI loginfail;
                    loginfail.type = UI_str;
                    loginfail.addconststring("登陆失败，用户名或密码错误");
                    loginpage.emplace_back(loginfail);
                    last_fail = 1;
                }
                else if(success == 1)
                {
                    userpage(0);
                }
            }
        }
    }
}

void _library::userpage(bool guest)
{
    //不用用户权限不同，用户菜单不同
    std::vector<_UI> userpage;
    if(guest)
    {
        _UI _tittle;
        _tittle.type = UI_str;
        _tittle.addconststring("用户菜单",1);
        _tittle.addconststring("访客模式");
        userpage.emplace_back(_tittle);
    }
    else if(users.loginedUser->isadmin)
    {
        _UI _tittle;
        _tittle.type = UI_str;
        _tittle.addconststring("用户菜单",1);
        _tittle.addconststring("管理员模式");
        userpage.emplace_back(_tittle);
    }
    else
    {
        _UI _tittle;
        _tittle.type = UI_str;
        _tittle.addconststring("用户菜单",1);
        _tittle.addconststring("读者模式");
        userpage.emplace_back(_tittle);
    }

    _UI menu;
    menu.type = UI_selector;
    menu.specialSelector = -1;
    menu.addconststring("查询图书",1);
    if(guest)
    {
        goto MENU_COMPLETE;
    }
    //这一部分是普通用户的菜单
    menu.addconststring("借阅图书",1);
    menu.addconststring("查看我的借阅",1);
    menu.addconststring("修改用户名",1);
    menu.addconststring("修改密码",1);
    if(!users.loginedUser->isadmin)
    {
        goto MENU_COMPLETE;
    }
    //下面是管理员菜单
    menu.addconststring("添加图书",1);
    menu.addconststring("修改图书信息",1);
    menu.addconststring("删除图书",1);
    menu.addconststring("添加管理员",1);
MENU_COMPLETE:
    userpage.emplace_back(menu);

    while(1)
    {
        clearUI();
        PrintUI(userpage);
        int m_key_pressed = KeyboardOperate();
        if(m_key_pressed == KEY_UP)
        {
            if(userpage[1].selectNum == 0)
                userpage[1].selectNum = userpage[1].data.size() - 2;
            else
                userpage[1].selectNum -= 1;
        }
        else if(m_key_pressed == KEY_DOWN)
        {
            if(userpage[1].selectNum == userpage[1].data.size() - 2)
                userpage[1].selectNum = 0;
            else
                userpage[1].selectNum += 1;
        }
        else if(m_key_pressed == KEY_ESC)
        {
            users.logout();
            return;
        }
        else
        {
            if(userpage[1].selectNum == 0)
            {
                searchpage(guest);
            }
            else if(userpage[1].selectNum == 1)
            {
                borrowpage();
            }
            else if(userpage[1].selectNum == 2)
            {
                returnpage();
            }
            else if(userpage[1].selectNum == 3)
            {
                usernameEditPage();
            }
            else if(userpage[1].selectNum == 4)
            {
                passwordEditPage();
            }
            else if(userpage[1].selectNum == 5)
            {
                addbookPage();
            }
            else if(userpage[1].selectNum == 6)
            {
                editbookPage();
            }
            else if(userpage[1].selectNum == 7)
            {
                delbookPage();
            }
            else if(userpage[1].selectNum == 8)
            {
                addadminPage();
            }
        }
    }
}

void _library::searchpage(bool guest)
{
    _UI tittle;
    tittle.type = UI_str;
    tittle.specialSelector = -1;
    tittle.addconststring("图书搜索与借阅");
    _UI m_searcher;
    m_searcher.type = UI_selector;
    std::string keyword;
    std::vector<SearchOutput> result;
    while(1)
    {
        clearUI();
        tittle.show();
        m_searcher.clear();
        m_searcher.addconststring("关键词：");
        m_searcher.addstr(&keyword,1);
        //result = books.search(keyword);
        if(result.empty())
        {
            m_searcher.addconststring("无匹配结果",1);
        }
        else
        {
            for(int i = 0;i < result.size();i++)
            {
                m_searcher.addconststring("置信度：");
                m_searcher.addfloat(&result[i].confidance);
                m_searcher.addconststring("书名:");
                m_searcher.addstr(&result[i].ptr->name,1);
            }
        }
        m_searcher.show();
        int m_key_pressed = KeyboardOperate();
        std::cout << "\n" << m_key_pressed << "\n";
        if(m_key_pressed == KEY_UP)
        {
            if(m_searcher.selectNum == 0)
                m_searcher.selectNum = m_searcher.data.size() - 2;
            else
                m_searcher.selectNum -= 1;
        }
        else if(m_key_pressed == KEY_DOWN)
        {
            if(m_searcher.selectNum == m_searcher.data.size() - 2)
                m_searcher.selectNum = 0;
            else
                m_searcher.selectNum += 1;
        }
        else if(m_key_pressed == KEY_ESC)
        {
            return;
        }
        else
        {
            //看一下选中了什么，如果是输入框，那就调一个输入，如果是书，那就展示详情
            if(m_searcher.selectNum == 0)
            {
                clearUI();
                std::cout << "关键词：";
                std::cin >>keyword;
                if(!keyword.empty())
                    result = books.search(keyword);
                //如果输入了新的书名，那就执行一下搜索
            }
            else
            {
                //如果是某本书，那就展示一下书籍详情，如果不是访客模式，还可以顺道展示别的菜单
                bookinfoPage(result[m_searcher.selectNum - 1].ptr);
            }
        }
    }
}

void _library::bookinfoPage(book* ptr)
{
    _UI info;
    info.type = UI_selector;
    info.specialSelector = -1;
    info.selectNum = 0;
    info.addconststring("书名:");
    info.addstr(&ptr->name,1);
    info.addconststring("编号：");
    info.addint(&ptr->ID,1);
    info.addconststring("总量：");
    info.addint(&ptr->number,1);
    info.addconststring("已出借数量：");
    info.addint(&ptr->borrownumber,1);
    info.addconststring("是否允许出借：");
    info.addbool(&ptr->allowborrow,1);
    info.addconststring("是否被删除：");
    info.addbool(&ptr->deleted,1);
    if(users.loginedUser == NULL)
        goto UI_COMPLETE;
    info.addconststring("借阅",1);
    if(!users.loginedUser->isadmin)
        goto UI_COMPLETE;
    info.addconststring("删除本书",1);
    info.addconststring("修改书籍允许借阅状态",1);
UI_COMPLETE:

    while(1)
    {
        clearUI();
        info.show();

        int m_key_pressed = KeyboardOperate();
        if(m_key_pressed == KEY_UP)
        {
            if(info.selectNum == 0)
                info.selectNum = info.data.size() - 1;
            else
                info.selectNum -= 1;
        }
        else if(m_key_pressed == KEY_DOWN)
        {
            if(info.selectNum == info.data.size() - 1)
                info.selectNum = 0;
            else
                info.selectNum += 1;
        }
        else if(m_key_pressed == KEY_ESC)
        {
            return;
        }
        else
        {
            if(info.selectNum == 6)
            {
                if(ptr->allowborrow)
                {
                    std::cout << "输入yes确认借阅\n";
                    std::string confirm;
                    std::cin >> confirm;
                    if(confirm == "yes")
                    {
                        users.loginedUser->borrowbook(ptr);
                        books.borrowBook(ptr);
                    }
                }
            }
            else if(info.selectNum == 7)
            {
                if(!(ptr->tobedel && ptr->deleted))
                {
                    std::cout << "输入yes确认删除\n";
                    std::string confirm;
                    std::cin >> confirm;
                    if(confirm == "yes")
                    {
                        ptr->tobedel = 1;
                    }
                }
            }
            else if(info.selectNum == 8)
            {
                if(!(ptr->tobedel && ptr->deleted))
                {
                    std::cout << "输入允许或不允许切换状态\n";
                    std::string confirm;
                    std::cin >> confirm;
                    if(confirm == "允许")
                    {
                        ptr->allowborrow = 1;
                    }
                    else if(confirm == "不允许")
                    {
                        ptr->allowborrow = 0;
                    }
                }
            }
            else if(users.loginedUser->isadmin)
            {
                if(info.selectNum == 0)
                {
                    std::cout << "请输入新名称";
                    std::cin >> ptr->name;
                }
                else if(info.selectNum == 2)
                {
                    std::cout << "请输入总量（不可小于当前已借出数量）：";
                    int middle = 0;
                    std::cin >> middle;
                    if(middle > ptr->borrownumber)
                        ptr->number = middle;

                }
                else if(info.selectNum == 4)
                {
                    ptr->allowborrow = !ptr->allowborrow;
                    if(ptr->tobedel || ptr->deleted)
                        ptr->allowborrow = 0;
                }
            }
        }
    }
}

void _library::borrowpage()
{
    _UI tittle;
    tittle.type = UI_str;
    tittle.specialSelector = -1;
    tittle.addconststring("图书搜索与借阅");
    _UI m_searcher;
    std::string keyword;
    std::vector<SearchOutput> result;
    while(1)
    {
        clearUI();
        tittle.show();
        m_searcher.addconststring("关键词：");
        m_searcher.addstr(&keyword,1);
        //result = books.search(keyword);
        if(result.empty())
        {
            m_searcher.addconststring("无匹配结果",1);
        }
        else
        {
            for(int i = 0;i < result.size();i++)
            {
                m_searcher.addconststring("置信度：");
                m_searcher.addfloat(&result[i].confidance);
                m_searcher.addconststring("书名:");
                m_searcher.addstr(&result[i].ptr->name,1);
            }
        }
        m_searcher.show();
        int m_key_pressed = KeyboardOperate();
        std::cout << "\n" << m_key_pressed << "\n";
        if(m_key_pressed == KEY_UP)
        {
            if(m_searcher.selectNum == 0)
                m_searcher.selectNum = m_searcher.data.size() - 1;
            else
                m_searcher.selectNum -= 1;
        }
        else if(m_key_pressed == KEY_DOWN)
        {
            if(m_searcher.selectNum == m_searcher.data.size() - 1)
                m_searcher.selectNum = 0;
            else
                m_searcher.selectNum += 1;
        }
        else if(m_key_pressed == KEY_ESC)
        {
            return;
        }
        else
        {
            //看一下选中了什么，如果是输入框，那就调一个输入，如果是书，那就借阅
            if(m_searcher.selectNum == 0)
            {
                clearUI();
                std::cout << "关键词：";
                std::cin >>keyword;
                if(!keyword.empty())
                    result = books.search(keyword);
                //如果输入了新的书名，那就执行一下搜索
            }
            else
            {
                //
                if(!result[m_searcher.selectNum - 1].ptr->allowborrow)
                    continue;
                std::cout << "是否确认借阅" << result[m_searcher.selectNum - 1].ptr->name << std::endl;
                std::cout << "输入yes确认借阅：";
                std::string confirm;
                std::cin >> confirm;
                if(confirm == "yes")
                {
                    books.borrowBook(result[m_searcher.selectNum - 1].ptr);
                    users.loginedUser->borrowbook(result[m_searcher.selectNum - 1].ptr);
                }
            }
        }
    }
}

void _library::returnpage()
{
    std::vector<_UI> page;
    _UI tittle;
    tittle.type = UI_str;
    tittle.specialSelector = -1;
    tittle.addconststring("请选择要返还的书");
    page.emplace_back(tittle);
    _UI selector;
    selector.type = UI_selector;
    selector.specialSelector = -1;
    selector.selectNum = 0;
    for(int i = 0;i < users.loginedUser->borrowedbooks.size();i++)
    {
        selector.addconststring("书名：");
        selector.addstr(&users.loginedUser->borrowedbooks[i]->name,1);
    }
    page.emplace_back(selector);

    while(1)
    {
        clearUI();
        PrintUI(page);

        int m_key_pressed = KeyboardOperate();
        if(m_key_pressed == KEY_UP)
        {
            if(page[1].selectNum == 0)
                page[1].selectNum = page[1].data.size() - 1;
            else
                page[1].selectNum -= 1;
        }
        else if(m_key_pressed == KEY_DOWN)
        {
            if(page[1].selectNum == page[1].data.size() - 1)
                page[1].selectNum = 0;
            else
                page[1].selectNum += 1;
        }
        else if(m_key_pressed == KEY_ESC)
        {
            return;
        }
        else
        {
            std::cout << "输入yes确认归还本书:";
            std::string confirm;
            std::cin >> confirm;
            if(confirm == "yes")
            {
                book* middle = users.loginedUser->borrowedbooks[page[1].selectNum];
                books.returnBook(middle);
                users.loginedUser->returnbook(users.loginedUser->borrowedbooks[page[1].selectNum]);
                page[1].clear();
                for(int i = 0;i < users.loginedUser->borrowedbooks.size();i++)
                {
                    page[1].addconststring("书名：");
                    page[1].addstr(&users.loginedUser->borrowedbooks[i]->name,1);
                }
            }
        }
    }
}

void _library::listAllBorrow()
{
    std::vector<_UI> page;
    _UI tittle;
    tittle.type = UI_str;
    tittle.specialSelector = -1;
    tittle.addconststring("我的借阅");
    _UI selector;
    selector.type = UI_selector;
    selector.specialSelector = -1;
    selector.selectNum = 0;
    for(int i = 0;i < users.loginedUser->borrowedbooks.size();i++)
    {
        selector.addconststring("书名：");
        selector.addstr(&users.loginedUser->borrowedbooks[i]->name,1);
    }
    page.emplace_back(selector);
    while (1)
    {
        clearUI();
        PrintUI(page);

        int m_key_pressed = KeyboardOperate();
        if(m_key_pressed == KEY_UP)
        {
            if(page[1].selectNum == 0)
                page[1].selectNum = page[1].data.size() - 1;
            else                
                page[1].selectNum -= 1;
        }
        else if(m_key_pressed == KEY_DOWN)
        {
            if(page[1].selectNum == page[1].data.size() - 1)
                page[1].selectNum = 0;
            else                
                page[1].selectNum += 1;
        }
        else if(m_key_pressed == KEY_ESC)
        {
            return;
        }
    }
}

void _library::usernameEditPage()
{
    std::vector<_UI> page;
    std::string name;
    _UI tittle;
    tittle.type = UI_str;
    tittle.addconststring("用户名修改页面",1);
    tittle.addconststring("当前用户名:",1);
    tittle.addstr(&users.loginedUser->username);
    page.emplace_back(tittle);

    _UI selector;
    selector.type = UI_selector;
    selector.specialSelector = -1;
    selector.selectNum = 0;
    selector.addconststring("请输入新用户名");
    selector.addstr(&name,1);
    selector.addconststring("确认修改",1);
    selector.addconststring("完成",1);
    page.emplace_back(selector);

    while(1)
    {
        clearUI();
        PrintUI(page);

        int m_key_pressed = KeyboardOperate();
        if(m_key_pressed == KEY_UP)
        {
            if(page[1].selectNum == 0)
                page[1].selectNum = page[1].data.size() - 1;
            else
                page[1].selectNum -= 1;
        }
        else if(m_key_pressed == KEY_DOWN)
        {
            if(page[1].selectNum == page[1].data.size() - 1)
                page[1].selectNum = 0;
            else
                page[1].selectNum += 1;
        }
        else if(m_key_pressed == KEY_ESC)
        {
            return;
        }
        else        
        {
            if(page[1].selectNum == 0)
            {
                std::cout << "请输入新用户名：";
                std::string middle;
                std::cin >> middle;
                if(!middle.empty())
                    name = middle;
            }
            else if(page[1].selectNum == 1)
            {
                users.loginedUser->username = name;
            }
            else if(page[1].selectNum == 2)
            {
                return;
            }
        }
    }
}

void _library::passwordEditPage()
{
    std::vector<_UI> page;
    std::string oldpassword;
    std::string newpassword;
    bool lastfailed = 0;
    _UI tittle;
    tittle.type = UI_str;
    tittle.addconststring("用户名修改页面",1);
    page.emplace_back(tittle);

    _UI selector;
    selector.type = UI_selector;
    selector.specialSelector = -1;
    selector.selectNum = 0;
    selector.addconststring("旧密码：");
    selector.addstr(&oldpassword,1);
    selector.addconststring("新密码：");
    selector.addstr(&newpassword,1);
    selector.addconststring("确认修改",1);
    selector.addconststring("完成",1);
    page.emplace_back(selector);

    while(1)
    {
        clearUI();
        PrintUI(page);

        int m_key_pressed = KeyboardOperate();
        if(m_key_pressed == KEY_UP)
        {
            if(page[1].selectNum == 0)
                page[1].selectNum = page[1].data.size() - 1;
            else
                page[1].selectNum -= 1;
        }
        else if(m_key_pressed == KEY_DOWN)
        {
            if(page[1].selectNum == page[1].data.size() - 1)
                page[1].selectNum = 0;
            else
                page[1].selectNum += 1;
        }
        else if(m_key_pressed == KEY_ESC)
        {
            return;
        }
        else        
        {
            if(page[1].selectNum == 0)
            {
                std::cout << "请输入旧密码：";
                std::string middle;
                std::cin >> middle;
                if(!middle.empty())
                    oldpassword = middle;
            }
            else if(page[1].selectNum == 1)
            {
                std::cout << "请输入新密码：";
                std::string middle;
                std::cin >> middle;
                if(!middle.empty())
                    newpassword = middle;
            }
            else if(page[1].selectNum == 2)
            {
                bool success = 0;
                if(oldpassword.empty() || newpassword.empty())
                    success = 0;
                else
                    success = users.loginedUser->ChangePassword(oldpassword,newpassword);
                if(page.size() == 3)
                {
                    page.erase(page.end());
                }
                if(success)
                {
                    _UI suc;
                    suc.type = UI_str;
                    suc.addconststring("修改成功");
                    page.emplace_back(suc);
                }
                else
                {
                    _UI suc;
                    suc.type = UI_str;
                    suc.addconststring("修改失败");
                    page.emplace_back(suc);
                }
            }
            else if(page[1].selectNum == 3)
            {
                return;
            }
        }
    }
}

void _library::addbookPage()
{
    std::string name;
    int number = 0;
    _UI info;
    info.type = UI_selector;
    info.specialSelector = -1;
    info.selectNum = 0;
    info.addconststring("书名:");
    info.addstr(&name,1);
    info.addconststring("总量：");
    info.addint(&number,1);
    info.addconststring("添加");
    while(1)
    {
        clearUI();
        info.show();

        int m_key_pressed = KeyboardOperate();
        if(m_key_pressed == KEY_UP)
        {
            if(info.selectNum == 0)
                info.selectNum = info.data.size() - 1;
            else
                info.selectNum -= 1;
        }
        else if(m_key_pressed == KEY_DOWN)
        {
            if(info.selectNum == info.data.size() - 1)
                info.selectNum = 0;
            else
                info.selectNum += 1;
        }
        else if(m_key_pressed == KEY_ESC)
        {
            return;
        }
        else        
        {
            if(info.selectNum == 0)
            {
                std::cout << "请输入书名：";
                std::cin >> name;
            }
            else if(info.selectNum == 1)
            {
                std::cout << "请输入数量";
                int middle = 0;
                std::cin >> middle;
                if(middle > -1)
                number = middle;
            }
            else if(info.selectNum == 2)
            {
                if(!name.empty())
                    books.addbook(book(name,number));
            }
        }
    }
}

void _library::editbookPage()
{
    _UI tittle;
    tittle.type = UI_str;
    tittle.specialSelector = -1;
    tittle.addconststring("图书修改");
    _UI m_searcher;
    m_searcher.type = UI_selector;
    std::string keyword;
    std::vector<SearchOutput> result;
    while(1)
    {
        clearUI();
        tittle.show();
        m_searcher.clear();
        m_searcher.addconststring("关键词：");
        m_searcher.addstr(&keyword,1);
        //result = books.search(keyword);
        if(result.empty())
        {
            m_searcher.addconststring("无匹配结果",1);
        }
        else
        {
            for(int i = 0;i < result.size();i++)
            {
                m_searcher.addconststring("置信度：");
                m_searcher.addfloat(&result[i].confidance);
                m_searcher.addconststring("书名:");
                m_searcher.addstr(&result[i].ptr->name,1);
            }
        }
        m_searcher.show();
        int m_key_pressed = KeyboardOperate();
        std::cout << "\n" << m_key_pressed << "\n";
        if(m_key_pressed == KEY_UP)
        {
            if(m_searcher.selectNum == 0)
                m_searcher.selectNum = m_searcher.data.size() - 2;
            else
                m_searcher.selectNum -= 1;
        }
        else if(m_key_pressed == KEY_DOWN)
        {
            if(m_searcher.selectNum == m_searcher.data.size() - 2)
                m_searcher.selectNum = 0;
            else
                m_searcher.selectNum += 1;
        }
        else if(m_key_pressed == KEY_ESC)
        {
            return;
        }
        else
        {
            //看一下选中了什么，如果是输入框，那就调一个输入，如果是书，那就展示详情
            if(m_searcher.selectNum == 0)
            {
                clearUI();
                std::cout << "关键词：";
                std::cin >>keyword;
                if(!keyword.empty())
                    result = books.search(keyword);
                //如果输入了新的书名，那就执行一下搜索
            }
            else
            {
                //如果是某本书，那就展示一下书籍详情，如果不是访客模式，还可以顺道展示别的菜单
                bookinfoPage(result[m_searcher.selectNum - 1].ptr);
            }
        }
    }
}

void _library::delbookPage()
{
    _UI tittle;
    tittle.type = UI_str;
    tittle.specialSelector = -1;
    tittle.addconststring("图书搜索与借阅");
    _UI m_searcher;
    std::string keyword;
    std::vector<SearchOutput> result;
    while(1)
    {
        clearUI();
        tittle.show();
        m_searcher.addconststring("关键词：");
        m_searcher.addstr(&keyword,1);
        //result = books.search(keyword);
        if(result.empty())
        {
            m_searcher.addconststring("无匹配结果",1);
        }
        else
        {
            for(int i = 0;i < result.size();i++)
            {
                m_searcher.addconststring("置信度：");
                m_searcher.addfloat(&result[i].confidance);
                m_searcher.addconststring("书名:");
                m_searcher.addstr(&result[i].ptr->name,1);
            }
        }
        m_searcher.show();
        int m_key_pressed = KeyboardOperate();
        std::cout << "\n" << m_key_pressed << "\n";
        if(m_key_pressed == KEY_UP)
        {
            if(m_searcher.selectNum == 0)
                m_searcher.selectNum = m_searcher.data.size() - 1;
            else
                m_searcher.selectNum -= 1;
        }
        else if(m_key_pressed == KEY_DOWN)
        {
            if(m_searcher.selectNum == m_searcher.data.size() - 1)
                m_searcher.selectNum = 0;
            else
                m_searcher.selectNum += 1;
        }
        else if(m_key_pressed == KEY_ESC)
        {
            return;
        }
        else
        {
            //看一下选中了什么，如果是输入框，那就调一个输入，如果是书，那就搜
            if(m_searcher.selectNum == 0)
            {
                clearUI();
                std::cout << "关键词：";
                std::cin >>keyword;
                if(!keyword.empty())
                    result = books.search(keyword);
                //如果输入了新的书名，那就执行一下搜索
            }
            else
            {
                if(!result[m_searcher.selectNum - 1].ptr->deleted)
                {
                    if(!result[m_searcher.selectNum - 1].ptr->tobedel)
                    {
                        if(result[m_searcher.selectNum - 1].ptr->borrownumber == 0)
                        {
                            result[m_searcher.selectNum - 1].ptr->deleted = 1;
                            result[m_searcher.selectNum - 1].ptr->allowborrow = 0;
                            result[m_searcher.selectNum - 1].ptr->number = 0;
                        }
                        else
                        {
                            result[m_searcher.selectNum - 1].ptr->tobedel = 1;
                            result[m_searcher.selectNum - 1].ptr->allowborrow = 0;
                        }
                    }
                }
            }
        }
    }
}

void _library::addadminPage()
{
    std::vector<_UI> page;
    std::string name;
    std::string password;
    _UI tittle;
    tittle.type = UI_str;
    tittle.addconststring("添加管理员页面");
    page.emplace_back(tittle);

    _UI selector;
    selector.type = UI_selector;
    selector.specialSelector = -1;
    selector.selectNum = 0;
    selector.addconststring("用户名");
    selector.addstr(&name,1);
    selector.addconststring("密码：");
    selector.addstr(&password,1);
    selector.addconststring("注册",1);
    selector.addconststring("退出",1);
    page.emplace_back(selector);

    while(1)
    {
        clearUI();
        PrintUI(page);

        int m_key_pressed = KeyboardOperate();
        if(m_key_pressed == KEY_UP)
        {
            if(page[1].selectNum == 0)
                page[1].selectNum = page[1].data.size() - 1;
            else
                page[1].selectNum -= 1;
        }
        else if(m_key_pressed == KEY_DOWN)
        {
            if(page[1].selectNum == page[1].data.size() - 1)
                page[1].selectNum = 0;
            else
                page[1].selectNum += 1;
        }
        else if(m_key_pressed == KEY_ESC)
        {
            return;
        }
        else        
        {
            if(page[1].selectNum == 0)
            {
                std::string middle;
                std::cout << "请输入用户名";
                std::cin >> middle;
                if(!middle.empty())
                    name = middle;
            }
            else if(page[1].selectNum == 1)
            {
                std::string middle;
                std::cout << "请输入密码";
                std::cin >> middle;
                if(!middle.empty())
                    password = middle;
            }
            else if(page[1].selectNum == 2)
            {
                if(page.size() == 3)
                    page.erase(page.end());
                if(!(name.empty() || password.empty()))
                {
                    user newadmin(name,password);
                    newadmin.isadmin = 1;
                    users.adduser(newadmin);
                    _UI suc;
                    suc.type = UI_str;
                    suc.addconststring("添加成功");
                    page.emplace_back(suc);
                }
                else
                {
                    _UI fail;
                    fail.type = UI_str;
                    fail.addconststring("添加失败");
                    page.emplace_back(fail);
                }
            }
            else if(page[1].selectNum == 3)
            {
                return;
            }
        }        
    }
}

void _library::loadDataFromLocalDatabase(std::string bookdb,std::string userdb)
{
    books.loadDataFromLocalDataBase(bookdb);
    users.loadDataFromLocalDataBase(userdb);
}